package com.badibadi.fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.badibadi.activity.PicturePreviewActivity;
import com.badibadi.adapter.LogReadingPictureAdapter;
import com.badibadi.infos.Article_img_Model;
import com.badibadi.infos.BrowseAllPhoto_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.DisplayUtil;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.miloisbadboy.view.PullToRefreshView;
import com.miloisbadboy.view.PullToRefreshView.OnFooterRefreshListener;
import com.miloisbadboy.view.PullToRefreshView.OnHeaderRefreshListener;

public class LogReadingDanjiFragment1 extends BaseFragment implements
		OnHeaderRefreshListener, OnFooterRefreshListener {
	private static final String TAG = "LogReadingDanjiFragment1";
	static LogReadingDanjiFragment1 logReadingDanjiFragment1;
	// private XGridView gv;
	private PullToRefreshView mPullToRefreshView;
	private GridView gv;
	private int Page;
	private List<Article_img_Model> Modellist, list;
	private LogReadingPictureAdapter logReadingPictureAdapter;
	private String rid;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case 1:
					Utils.ExitPrgress(getActivity());
					try {
						Utils.showMessage(getActivity(), getResources()
								.getString(R.string.l_net_error));
					} catch (Exception e) {
						// TODO: handle exception
					}

					mPullToRefreshView.onFooterRefreshComplete();
					mPullToRefreshView.onHeaderRefreshComplete();
					break;
				case 2:
					try {
						Utils.ExitPrgress(getActivity());
						Modellist.addAll(list);
						logReadingPictureAdapter.notifyDataSetChanged();
						mPullToRefreshView.onFooterRefreshComplete();
						mPullToRefreshView.onHeaderRefreshComplete();
					} catch (Exception e) {
						// TODO: handle exception
					}

					//    ("跟新数据");
					break;
				case 3:
					Utils.ExitPrgress(getActivity());
					try {
						Utils.showMessage(getActivity(), getResources()
								.getString(R.string.l_xa10));
						mPullToRefreshView.onFooterRefreshComplete();
						mPullToRefreshView.onHeaderRefreshComplete();
					} catch (Exception e) {
						// TODO: handle exception
					}

					break;
				default:
					break;
				}
			} catch (Exception e) {
				   
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		   
		rid = getArguments().getString("rid");
		Modellist = new ArrayList<Article_img_Model>();
		logReadingPictureAdapter = new LogReadingPictureAdapter(getActivity(),
				Modellist);

	}// regionhomepage_import_layout2

	@Override
	public View onCreateView(final LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
//		   
		final View view = inflater.inflate(R.layout.a_xgridview, null);
		Page = 1;
		mPullToRefreshView = (PullToRefreshView) view
				.findViewById(R.id.main_pull_refresh_view);
		gv = (GridView) view.findViewById(R.id.xgridView11);
		// gv.setXGridViewListener(this);
		gv.setNumColumns(3);
		// gv.setHorizontalSpacing(DisplayUtil.dip2px(getActivity(), 5));
		gv.setPadding(DisplayUtil.dip2px(getActivity(), 5),
				DisplayUtil.dip2px(getActivity(), 5),
				DisplayUtil.dip2px(getActivity(), 5),
				DisplayUtil.dip2px(getActivity(), 5));
		// gv.setVerticalSpacing(DisplayUtil.dip2px(getActivity(), 5));
		gv.setAdapter(logReadingPictureAdapter);
		gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// TODO Auto-generated method stub
				List<BrowseAllPhoto_Model> l;
				l = logReadingPictureAdapter.getItemToBrowseAll();
				if (l != null) {
					Intent intent = new Intent(getActivity(),
							PicturePreviewActivity.class);
					intent.putExtra("picture_list", (Serializable) l);
					intent.putExtra("picture_postion", position);
					startActivity(intent);
				}

			}
		});
//		   
		Show_club(Page);
		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);
		return view;
	}

	private void Show_club(final int page) {
		// Utils.showPrgress(getActivity());
//		   
		new Thread(new Runnable() {
			@Override
			public void run() {
				list = new ArrayList<Article_img_Model>();
				String content = Utils.getRequest(Constants.BadiUrl
						+ "article_img?rid=" + rid + "&page=" + Page
						+ "&pageNum" + 8);
//				   
//						+ "article_img?rid=" + rid + "&page=" + Page
//						+ "&pageNum" + 8);
				if (content != null) {
					Results results = Utils.checkResult_NNN(getActivity(),
							content);
//					   ("结果是1:" + content);
//					   
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
//						   ("结果是:" + results.getRetmsg());
//						   
						try {
							list = JSONUtils.getListByJsonString(
									results.getRetmsg(),
									Article_img_Model.class);
							handler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						if (Page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						handler.sendEmptyMessage(3);
					}
				} else {
					if (Page > 1) {
						--Page;
					} else {
						Page = 1;
					}
					handler.sendEmptyMessage(1);
				}
			}
		}).start();
	}

	// /**
	// * 打开或管理悬浮对话框
	// *
	// * @param inflater
	// * @param v
	// */
	// public void OpenOrClosePopWindows(LayoutInflater inflater, View v) {
	// if (popup == null) {
	// View root = inflater.inflate(
	// R.layout.fragment_club_overview_popwindows, null);
	// popup = new PopupWindow(root, 300, 360);
	// popup.showAsDropDown(v);
	// } else {
	// popup.dismiss();
	// popup = null;
	// }
	// }
	// @Override
	// public void OnLoadMore() {
	// Show_club(++Page);
	// }
	public static LogReadingDanjiFragment1 getInstance() {
		if (logReadingDanjiFragment1 == null) {
			return new LogReadingDanjiFragment1();
		}
		return logReadingDanjiFragment1;
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		Show_club(++Page);
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		Page = 1;
		if (Modellist != null) {
			Modellist.clear();
		}
		Show_club(Page);
	}
}
